#include <stdio.h>
#include "screen_emulate.h"


void screen_draw_point(int x, int y) {
    if (x >= 0 && x < SCREEN_WIDTH && y >= 0 && y < SCREEN_HEIGHT) {
        screen_buff[x][y] = 0xff;
    }

}

void screen_clear(int sx, int sy, int ex, int ey) {
    int i, j;
    for (i = sx; i < ex && i < SCREEN_WIDTH; i++) {
        for (j = sy; j < ey && j < SCREEN_HEIGHT; j++) {
            screen_buff[i][j] = 0x00;
        }
    }
}

void screen_full_clear() {
    int i, j;
    for (i = 0; i < SCREEN_WIDTH; i++) {
        for (j = 0; j < SCREEN_HEIGHT; j++) {
            screen_buff[i][j] = 0x00;
        }
    }
}

void screen_fill(int sx, int sy, int ex, int ey) {
    int i, j;
    for (i = sx; i < ex && i < SCREEN_WIDTH; i++) {
        for (j = sy; j < ey && j < SCREEN_HEIGHT; j++) {
            screen_buff[i][j] = 0xff;
        }
    }
}


void screen_draw_line(int x1, int y1, int x2, int y2) {
    int t;
    int xerr = 0, yerr = 0, delta_x, delta_y, distance;
    int incx, incy, uRow, uCol;
    delta_x = x2 - x1;
    delta_y = y2 - y1;
    uRow = x1;
    uCol = y1;
    if (delta_x > 0)incx = 1;
    else if (delta_x == 0)incx = 0;
    else {
        incx = -1;
        delta_x = -delta_x;
    }
    if (delta_y > 0)incy = 1;
    else if (delta_y == 0)incy = 0;
    else {
        incy = -1;
        delta_y = -delta_y;
    }
    if (delta_x > delta_y)distance = delta_x;
    else distance = delta_y;
    for (t = 0; t <= distance + 1; t++) {
        screen_draw_point(uRow, uCol);
        xerr += delta_x;
        yerr += delta_y;
        if (xerr > distance) {
            xerr -= distance;
            uRow += incx;
        }
        if (yerr > distance) {
            yerr -= distance;
            uCol += incy;
        }
    }
}

void screen_draw_rectangle(int x1, int y1, int x2, int y2) {
    screen_draw_line(x1, y1, x2, y1);
    screen_draw_line(x1, y1, x1, y2);
    screen_draw_line(x1, y2, x2, y2);
    screen_draw_line(x2, y1, x2, y2);
}

void screen_draw_circle(int x0, int y0, int r) {
    int a, b;
    int di;
    a = 0;
    b = r;
    di = 3 - (r << 1);
    while (a <= b) {
        screen_draw_point(x0 + a, y0 - b);             //5
        screen_draw_point(x0 + b, y0 - a);             //0
        screen_draw_point(x0 + b, y0 + a);             //4
        screen_draw_point(x0 + a, y0 + b);             //6
        screen_draw_point(x0 - a, y0 + b);             //1
        screen_draw_point(x0 - b, y0 + a);
        screen_draw_point(x0 - a, y0 - b);             //2
        screen_draw_point(x0 - b, y0 - a);             //7
        a++;
        if (di < 0)di += 4 * a + 6;
        else {
            di += 10 + 4 * (a - b);
            b--;
        }
    }
}

void screen_show_num(int x, int y, int num, int len, int size) {

}

void screen_show_string(int x, int y, int width, int height, int font_size, int *str) {

}


void screen_show() {
    int i, j;
    for (i = 0; i < SCREEN_HEIGHT; i++) {
        for (j = 0; j < SCREEN_WIDTH; j++) {
            if (screen_buff[i][j] > 0) {
                printf("*");
            } else {
                printf("-");
            }
        }
        printf("\r\n");
    }
}








